package org.desktop.web.client.gui.models;

import org.desktop.web.client.custom.widget.AbstractWindow;
import org.desktop.web.client.images.Resources;

import com.google.gwt.user.client.rpc.IsSerializable;
import com.google.gwt.user.client.ui.IsWidget;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.button.ButtonBar;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;

public abstract class AbstractWindowForm<B extends IsSerializable> extends
		AbstractWindow {
	public enum OperationType {
		INSERT, UPDATE;
	}

	public enum Buttons {

		SAVE, CANCEL

	}

	private OperationType operationType;

	private FramedPanel mainPanel = new FramedPanel();

	private ButtonBar buttonBar = new ButtonBar();

	/**
	 * Crea il layout del form
	 * 
	 * @return
	 */
	public abstract IsWidget createLayout();

	public abstract SelectHandler getInsertHandler();

	public abstract SelectHandler getUpdateHandler();

	public AbstractWindowForm(OperationType operationType) {
		this.operationType = operationType;
		this.mainPanel.setHeaderVisible(false);
		setModal(true);
		setHeaderVisible(true);

	}

	@Override
	public final void init() {
		VerticalLayoutContainer vlc = new VerticalLayoutContainer();
		buttonBar.setPack(BoxLayoutPack.CENTER);
		TextButton saveButton = new TextButton();
		saveButton.setText("Save");
		saveButton.setIcon(Resources.IMAGES.save());
		saveButton
				.addSelectHandler(OperationType.INSERT.equals(operationType) ? getInsertHandler()
						: getUpdateHandler());
		buttonBar.add(saveButton);

		vlc.add(createLayout(), new VerticalLayoutData(1, 1));
		vlc.add(buttonBar, new VerticalLayoutData(1, -1));
		mainPanel.add(vlc);

		this.add(mainPanel);
	}

	protected OperationType getOperationType() {
		return operationType;
	}

}
